//
//  RecommendViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/12/6.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "RecommendViewController.h"
#import "ExtensionInfo.h"

@interface RecommendViewController ()
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation RecommendViewController
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"推荐榜";
    [self setupRefresh];
}
#pragma mark - 获取下面的推荐阅读列表
- (void)creatDataList{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[NSString stringWithFormat:@"%ld",(long)self.pageNo] forKey:@"page"];
//    [dic setValue:@"1" forKey:@"page"];//
    [dic setValue:KREFRESHROWS forKey:@"rows"]; //
    [dic setValue:@"1" forKey:@"extension"];   //推荐阅读
    
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppHomePageGetHomePage] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            if (self.pageNo == 1) {
                [self.dataArray removeAllObjects];
            }
            NSArray *array = [NSArray arrayWithArray:response[@"list"]];
            if (array.count) {
                [self.dataArray addObjectsFromArray:[ExtensionInfo mj_objectArrayWithKeyValuesArray:response[@"list"]]];
                [self refreshCount:self.dataArray.count noDataText:@"还没有推荐的" totalRow:response[@"totalRow"]];
                [self.tableView reloadData];
            }
        }
    }];
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        UIImageView *iconImage = [[UIImageView alloc]init];
        iconImage.frame = CGRectMake( 10, 10, 110, 60);
        iconImage.tag = 1000;
        iconImage.clipsToBounds = YES;
        iconImage.layer.cornerRadius = 5;
        [cell.contentView addSubview:iconImage];
        
        UILabel *nameLabel = [[UILabel alloc]init];
        nameLabel.tag = 1001;
        nameLabel.numberOfLines = 2;
        //adjustsFontSizeToFitWidth
        nameLabel.textAlignment = NSTextAlignmentLeft;
        nameLabel.font = [UIFont systemFontOfSize:16];
        nameLabel.textColor = [UIColor blackColor];
        [cell.contentView addSubview:nameLabel];
        
        UILabel *reportLabel = [[UILabel alloc]init];
        reportLabel.tag = 1002;
        reportLabel.textAlignment = NSTextAlignmentLeft;
        reportLabel.font = [UIFont systemFontOfSize:12];
        reportLabel.textColor = [UIColor darkTextColor];
        [cell.contentView addSubview:reportLabel];
        
        UILabel *attentionLabel = [[UILabel alloc]init];
        attentionLabel.tag = 1003;
        attentionLabel.textAlignment = NSTextAlignmentRight;
        attentionLabel.font = [UIFont systemFontOfSize:12];
        attentionLabel.textColor = [UIColor darkTextColor];
        [cell.contentView addSubview:attentionLabel];
    }
    ExtensionInfo *extensionInfo = self.dataArray[indexPath.row];
    UIImageView *iconImage = [cell.contentView viewWithTag:1000];
    [iconImage sd_setImageWithURL:[NSURL URLWithString:extensionInfo.pathUrl] placeholderImage:KRECOMMENDLISTDEFAULT];
    
    UILabel *nameLabel = [cell.contentView viewWithTag:1001];
    nameLabel.frame = CGRectMake(CGRectGetMaxX(iconImage.frame) + 5, CGRectGetMinY(iconImage.frame), kScreenW - CGRectGetMaxX(iconImage.frame) - 10, [Global getSizeOfString:extensionInfo.title maxWidth:kScreenW - CGRectGetMaxX(iconImage.frame) - 10 maxHeight:40 withFontSize:16.6].height);
    nameLabel.text = extensionInfo.title;
    
    UILabel *reportLabel = [cell.contentView viewWithTag:1002];
    reportLabel.frame = CGRectMake(CGRectGetMinX(nameLabel.frame), CGRectGetMaxY(iconImage.frame) - 15, CGRectGetWidth(nameLabel.frame) / 2, 15);
    reportLabel.text = [extensionInfo.source isEqualToString:@"1"]? @"与时校园":@"外网链接";
    
    UILabel *attentionLabel = [cell.contentView viewWithTag:1003];
    attentionLabel.frame = CGRectMake(CGRectGetMaxX(nameLabel.frame) - CGRectGetWidth(nameLabel.frame) / 2, CGRectGetMaxY(iconImage.frame) - 15, CGRectGetWidth(nameLabel.frame) / 2, 15);
    attentionLabel.text = [NSString stringWithFormat:@"%@阅读数",extensionInfo.readCount];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
//    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    ExtensionInfo *extensionInfo = self.dataArray[indexPath.row];
//    [dic setValue:extensionInfo.fileEntryId forKey:@"fileEntryId"];   //主健
//
//    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppHomePageInfoRecord] andParmas:dic andComplition:^(id response, BOOL isuccess) {
//        if (isuccess) {
//            extensionInfo.readCount = [NSString stringWithFormat:@"%d",[extensionInfo.readCount intValue]+ 1];
//            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
//        }
//    }];
    
    WebViewController *webView = [[WebViewController alloc]initWithTitle:extensionInfo.title withRequest:[NSString stringWithFormat:@"%@/sys/home/gasStation?fileEntryId=%@",URL_BASE,extensionInfo.fileEntryId]];
    webView.enterWebViewBlock=^(){
        extensionInfo.readCount = [NSString stringWithFormat:@"%d",[extensionInfo.readCount intValue]+ 1];
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    };
    [self.navigationController pushViewController:webView animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectZero;
    view.backgroundColor = KBACKGROUNDCOLOR;
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
